package service

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"math/rand"
	"strconv"
	"time"
	"user-service/basic/config"
	__ "user-service/basic/proto"
	"user-service/model"
	"user-service/pkg"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// UserByCreate 注册
func (s *Server) UserByCreate(_ context.Context, in *__.UserByCreateReq) (*__.UserByCreateResp, error) {
	var user model.Users
	config.DB.Where(&model.Users{Username: in.Username}).Find(&user)
	if user.ID > 0 {
		return nil, errors.New("用户已存在")
	}
	if pkg.GetEncryption(in.Password) != pkg.GetEncryption(in.Confirm) {
		return nil, errors.New("密码不一致")
	}
	get := config.RDB.Get(context.Background(), "sms"+"register"+in.Username)
	if get.Val() != in.SendSms {
		return nil, errors.New("短信发送错误")
	}
	users := model.Users{
		Username: in.Username,
		Password: pkg.GetEncryption(in.Password),
	}
	config.DB.Create(&users)
	return &__.UserByCreateResp{
		UserId: int64(users.ID),
	}, nil
}

// UserByUsername 登录
func (s *Server) UserByUsername(_ context.Context, in *__.UserByUsernameReq) (*__.UserByUsernameResp, error) {
	var user model.Users
	config.DB.Where(&model.Users{Username: in.Username}).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	get := config.RDB.Get(context.Background(), "sms"+"login"+in.Username)
	if get.Val() != in.SendSms {
		return nil, errors.New("短信发送错误")
	}
	if pkg.GetEncryption(in.Password) != user.Password {
		return nil, errors.New("账号或者密码错误")
	}
	return &__.UserByUsernameResp{
		UserId: int64(user.ID),
	}, nil
}

// SendSms 短信
func (s *Server) SendSms(_ context.Context, in *__.SendSmsReq) (*__.SendSmsResp, error) {
	code := rand.Intn(9000) + 1000
	three := config.RDB.Get(context.Background(), "three"+in.Source+in.Username)
	if three.Val() >= "3" {
		return nil, errors.New("限制短信发送一分钟只能发送三次")
	}
	//sms, err := pkg.SendCallSms(in.Username, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code == "OK" {
	//	return nil, errors.New(*sms.Body.Message)
	//}
	//err = pkg.Mutual(in.Username, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}

	incr := config.RDB.Incr(context.Background(), "three"+in.Source+in.Username)
	if incr.Val() >= 3 {
		config.RDB.Expire(context.Background(), "three"+in.Source+in.Username, time.Minute*5)
	}
	config.RDB.Set(context.Background(), "sms"+in.Source+in.Username, code, time.Minute*1)

	return &__.SendSmsResp{}, nil
}
func (s *Server) RechargeOrder(_ context.Context, in *__.RechargeOrderReq) (*__.RechargeOrderResp, error) {
	var user model.Users
	userId := in.UserId
	config.DB.Where("id = ?", userId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	var member model.Memberships
	memberId := in.MemberId
	config.DB.Where("id = ?", memberId).Find(&member)
	if member.Id == 0 {
		return nil, errors.New("会员不存在")
	}

	orders := model.Orders{
		UserId:   int(userId),
		MemberId: int(memberId),
		OrderSn:  uuid.NewString(),
		Price:    int(in.Price),
	}
	config.DB.Create(&orders)
	aLiPay := pkg.ALiPay(uuid.NewString(), strconv.FormatInt(in.Price, 10))
	fmt.Println(aLiPay)
	return &__.RechargeOrderResp{}, nil
}
func (s *Server) MemberDetails(_ context.Context, in *__.MemberDetailsReq) (*__.MemberDetailsResp, error) {
	var member model.Memberships
	memberId := in.MemberId
	config.DB.Where("id = ?", memberId).Find(&member)
	if member.Id == 0 {
		return nil, errors.New("会员不存在")
	}

	return &__.MemberDetailsResp{
		MemberId: int64(member.Id),
		UserId:   int64(member.UserId),
		Level:    int64(member.Level),
	}, nil
}
